extends Node2D

@onready var target : Marker2D = $Target
const pre_enemy = preload("res://Enemy/Enemy.tscn")

var enemy_list : Array[int]
func _ready() -> void:
	target.global_position = Vector2(0,-30)
	
	var enemy = pre_enemy.instantiate()
	enemy.global_position = Vector2(0,100)
	enemy._init_data(target)
	add_child(enemy)
	enemy_list.append(enemy.get_instance_id())
	
	enemy = pre_enemy.instantiate()
	enemy.global_position = Vector2(-100,0)
	enemy._init_data(target)
	add_child(enemy)
	enemy_list.append(enemy.get_instance_id())
	
	
	pass
	
func _process(delta):
	#queue_redraw()
	pass

func _draw():
	return
	var enemy = instance_from_id(enemy_list[0])
	var v_a = enemy.target_pos
	draw_line(enemy.global_position,v_a + enemy.global_position,Color.GREEN)
	
	var enemy2 = instance_from_id(enemy_list[1])
	var v_b = -enemy2.target_pos
	draw_line(enemy.global_position,v_b + enemy.global_position,Color.GREEN)
	draw_circle(enemy2.global_position,40,Color.RED)
	
	#叠加向量
	var v_c_start = enemy.global_position
	var v_c_end = v_c_start + v_a + v_b
	draw_line(v_c_start,v_c_end,Color.GREEN)
	
	
	var A = enemy.global_position
	var C = enemy2.global_position
	
	
	
	
	
	
	
	#(a1-b1)^2+(a2-b2)^2=a^2,(a1-c1)^2+(a2-c2)^2=b^2;(b1-c1)^2+（b2-c2)^2=c^2
	if true :
		
		pass
	
	#让速向量
	var v_d_start = v_c_end
	var v_d_circle_p = (v_d_start - enemy2.global_position).normalized() * 40.0 + enemy2.global_position
	var v_d_end = (v_d_circle_p - v_d_start).normalized() * (v_d_circle_p - v_d_start).length() / 2.0 + v_d_start
	
	##让速向量过圆切线
	var v_f = Vector2(v_d_end.y-v_d_start.y,v_d_start.x-v_d_end.x)
#
	###y = kx + b
	var k = v_f.y / v_f.x
	var b = v_d_end.y - (k * v_d_end.x)
	
	var arr : PackedVector2Array = []
	for p in range(-500,500) :
		var x = v_d_end.x + p
		var y = k * x + b
		arr.append(Vector2(x,y))
	draw_polyline(arr,Color.CORAL,1.0)
	pass
